home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / latex209 / contrib / trees / tree-dvips / tree.script < prev   
Text File  |  1994-01-28  |  12KB  |  519 lines

  1. % grestore
  2. % special commands for setting up tree macros
  3. TeXDict begin
  4.  
  5. % the commands needed to get any of this working.  The first section
  6. % may need to be modified if different dvi to postscript programs are
  7. % used. 
  8.  
  9. /@beginspec {gsave texpsmatrix setmatrix treedict begin} def
  10. /@endspec {end grestore} def
  11.  
  12. %% definition of letter matrix
  13. %/testorient
  14. %  { gsave
  15. %    matrix defaultmatrix
  16. %    72 Resolution div dup neg 
  17. %    3 -1 roll scale          % set scaling to 1.
  18. %    310 -3005 
  19. %    3 -1 roll translate      % move origin to top (these are not exactly 1"
  20. %    Mtrx 
  21. %  } def
  22. %
  23.  
  24. % /Landscape false def 
  25.  
  26. % for landscape Landscape=true, letter Landscape=false
  27. /texpsmatrix 
  28. matrix defaultmatrix 
  29. isls {dup [0.0 -1.0 1.0 0.0 0.0 0.0] exch concatmatrix} if
  30. def
  31. userdict begin
  32. /bop-hook
  33. {/texpsmatrix 
  34. matrix defaultmatrix 
  35. isls {dup [0.0 -1.0 1.0 0.0 0.0 0.0] exch concatmatrix} if
  36. def}
  37. B
  38. end
  39. %/texpsmatrix matrix defaultmatrix def
  40. /treedict 200 dict def treedict begin
  41.  
  42. %  num pt num  (converts a number in TeX pts (72.27 pts/in) to
  43. %  postscript pts (72pts/in)
  44. /pt {72 mul 72.07 div} def
  45.  
  46. % end of first section
  47.  
  48. % definition of a node as a box containing the following information
  49. % The x y location of the lower left hand corner of the box
  50. % the height of the box (h) 
  51. % the width of the box (w)
  52. % nodemargin is added to all sides to ensure a boundary around the box
  53. % str w h d node -
  54. % (nodemargin is defined in the TeX file)
  55. /nodemargin 2 def
  56. /nodes 100 dict def
  57. /node { /dpth exch def
  58.            /hght exch def
  59.            /wdth exch def
  60.                   4 dict dup begin
  61.                  /x /y currentpoint 
  62.                        dpth sub nodemargin sub exch 3 1 roll def
  63.                     nodemargin sub def
  64.                  /h hght dpth add nodemargin dup add add def
  65.                  /w wdth nodemargin dup add add def
  66.                end nodes 3 1 roll put} def
  67.  
  68.  
  69. % commands to find various locations around the nodebox
  70. %
  71. %                 nodetopleft      nodetop    nodetopright
  72. %                           *--------*--------*
  73. %                           |                 |
  74. %                           |                 |
  75. %   farleft        nodeleft *  (nodebox)      * noderight    farright
  76. %                           |                 |
  77. %                           |                 |
  78. %                           *--------*--------*
  79. %                 nodebotleft     nodebottom  nodebotright
  80. %
  81. %
  82. %  farbotleft                     farbottom
  83.  
  84. % all of these take
  85. % dict nodebottom x1 y1   [dict has x y h w defined]
  86. /nodebottom {begin 
  87.                    x w 2 div add y
  88.                    end} def
  89. /nodetop {begin
  90.                 x w 2 div add y h add
  91.                 end} def
  92. /nodeleft {begin
  93.                  x y h 2 div add
  94.                 end} def
  95. /noderight {begin
  96.                   x w add y h 2 div add
  97.                   end} def
  98. /nodetopleft {begin
  99.         x y h add
  100.         end} def
  101. /nodetopright {begin
  102.          x w add y h add
  103.         end} def
  104. /nodebottomleft {begin
  105.         x y
  106.         end} def
  107. /nodebottomright {begin
  108.         x w add y
  109.         end} def
  110.  
  111. % some more locations of use
  112. /farright {begin
  113.         x w add depth add h 2 div y add
  114.     end} def
  115.  
  116. /farleft {begin
  117.         x depth sub h 2 div y add
  118.     end} def
  119.  
  120. /farbottom {begin
  121.         x w 2 div add y depth sub
  122.     end} def
  123.  
  124. /fartop {begin
  125.         x w 2 div add y h add depth add
  126.     end} def
  127.  
  128. /farbottomleft {begin
  129.                 x depth 45 cos mul sub y depth 45 sin mul sub
  130.     end} def
  131.  
  132. /farbottomright {begin
  133.                 x w add depth 45 cos mul add y depth 45 sin mul sub
  134.     end} def
  135.  
  136. /fartopright {begin
  137.                 x w add depth 45 cos mul add y h add depth 45 sin mul add
  138.     end} def
  139.  
  140. /fartopleft {begin
  141.                 x depth 45 cos mul sub y h add depth 45 sin mul add
  142.     end} def
  143.  
  144.  
  145. % connects two nodes with a straight line 
  146. % add location information (e.g., whether to go from
  147. % nodetop to nodeleft ...
  148.  
  149. % nodename1 loc1 nodename2 loc2 nodeconnect -
  150. % e.g., a nodetop b nodebottom nodeconnect
  151.  
  152. % to see if straight lines are indeed straight
  153. % x1 x2 alignpoint x1' x2'  finds out if the two numbers are less than
  154. % one pixel apart if so make x1'=x2'
  155.  
  156. /alignpoint {2 copy sub abs 1 le {add 2 div round dup} if} def
  157.  
  158. % nodelocation nodename getnodepoint x y 
  159. /getnode {nodes exch get} def
  160.  
  161. % xfrom yfrom xto yto nodeconnect -
  162. /nodeconnect {       gsave
  163.                transform 4 2 roll
  164.                        transform
  165.                exch 4 1 roll alignpoint
  166.                        4 2 roll alignpoint
  167.                        4 1 roll exch
  168.                        itransform moveto itransform lineto
  169.                        stroke
  170.                      grestore
  171.                      } def
  172.  
  173. % arrowheads
  174. % xtail ytail xtip ytip headthickness headlength arrow -
  175.  
  176. /arrowdict 14 dict def
  177. arrowdict begin
  178.     /mtrx matrix def
  179. end
  180.  
  181. /arrow {arrowdict begin
  182.     /headlength exch def
  183.     /halfheadthickness exch 2 div def
  184.     /tipy exch def /tipx exch def
  185.     /taily exch def /tailx exch def
  186.  
  187.     /dx tipx tailx sub def
  188.     /dy tipy taily sub def
  189.     /angle dy dx atan def
  190.     
  191.     /savematrix mtrx currentmatrix def
  192.     tipx tipy translate
  193.     angle rotate
  194.     0 0 moveto
  195.     headlength neg halfheadthickness neg lineto
  196.     headlength neg halfheadthickness lineto
  197.     closepath
  198. savematrix setmatrix
  199.     end } def
  200.  
  201.  
  202. % xfrom yfrom xto yto testcmd -
  203. % arrownodeconnect
  204. /arrownodeconnect{    gsave
  205.         transform 4 2 roll
  206.                 transform
  207.         exch 4 1 roll alignpoint
  208.                 4 2 roll alignpoint
  209.                 4 1 roll exch
  210.                 itransform 4 2 roll itransform 4 2 roll 4 copy moveto lineto
  211.         gsave
  212.         newpath
  213.         4 2 roll arrowwidth arrowlength arrow    
  214.         fill
  215.         grestore
  216.                 stroke
  217.                grestore
  218.         } def
  219.  
  220.  
  221. % connecting with a bar like
  222. %     _______________
  223. %     |             |
  224. % or like
  225. %     _______________
  226. %     |             |
  227. %     |           node
  228. %    node
  229.  
  230. % depth fromnode fromloc tonode toloc
  231. % depth x1 y1 x2 y2 barnodeconnect -
  232. /barnodeconnect {4 2 roll
  233.           2 copy moveto
  234.           5 -1 roll add
  235.           dup 3 1 roll
  236.           lineto
  237.           2 index exch
  238.           lineto lineto
  239.           stroke
  240.          } def
  241.  
  242. /arrowbarnodeconnect {4 2 roll
  243.           2 copy moveto
  244.           5 -1 roll add
  245.           dup 3 1 roll
  246.           lineto
  247.           2 index exch
  248.           4 copy lineto lineto
  249.         gsave
  250.         newpath
  251.         4 2 roll
  252.         arrowwidth arrowlength arrow    
  253.         fill
  254.         grestore
  255.          stroke
  256.          } def
  257.  
  258.  
  259. % Takes two nodes and forms a triangle
  260. %         node1
  261. %         /\
  262. %        /  \
  263. %        ----
  264. %       nodess
  265. %
  266. % fromnodename tonodename nodetriangle -
  267. /nodetriangle {
  268.         gsave
  269.                         exch
  270.             nodes exch get nodebottom moveto
  271.             dup
  272.             nodes exch get nodetopleft lineto
  273.             nodes exch get nodetopright lineto
  274.             closepath
  275.             stroke
  276.         grestore } def
  277.  
  278.  
  279. % not sure what I am using the following for
  280. %  x y x y slope num
  281. /slope {/y1 exch def
  282.     /x1 exch def
  283.     /y0 exch def
  284.     /x0 exch def
  285.     y1 y0 sub x1 x0 sub div
  286.     } def
  287.  
  288. % x y x y midpoint x y 
  289. /midpoint {/y1 exch def
  290.        /x1 exch def
  291.        /y0 exch def
  292.        /x0 exch def
  293.        x1 x0 sub abs
  294.        x1 x0 ge {x0 add} {x1 add} ifelse
  295.        y1 y0 sub abs
  296.        y1 y0 ge {y0 add} {y1 add} ifelse
  297.     } def
  298.  
  299.  
  300. % variant of curveto where x1 y1 = x2 y2
  301. /tancurveto {1 index exch
  302.     curveto} def
  303.  
  304. /nodetancurve {/depth exch def
  305.         /to exch def
  306.         /from exch def
  307.         gsave
  308.         nodes from get noderight moveto
  309.  
  310.         nodes to get noderight 
  311.         tancurveto
  312.         stroke
  313.         grestore } def
  314.  
  315.  
  316.  
  317. % the node curves draw curves between two points
  318. %
  319. %
  320.  
  321. % nodename rightcur x1 y1 x2 y2
  322. /rightcur {nodes exch get dup noderight 3 -1 roll farright} def
  323. /leftcur {nodes exch get dup nodeleft 3 -1 roll farleft} def
  324. /topcur {nodes exch get dup nodetop 3 -1 roll fartop} def
  325. /bottomcur {nodes exch get dup nodebottom 3 -1 roll farbottom} def
  326. /topleftcur {nodes exch get dup nodetopleft 3 -1 roll fartopleft} def
  327. /toprightcur {nodes exch get dup nodetopright 3 -1 roll fartopright} def
  328. /bottomleftcur {nodes exch get dup nodebottomleft 3 -1 roll farbottomleft} def
  329. /bottomrightcur {nodes exch get dup nodebottomright 3 -1 roll farbottomright} def
  330.  
  331. % (depth predefined) x1 y1 x2 y2 x3 y3 x4 y4 nodecurve -
  332. % where  nodename nodeposition produces x1 y1 x2 y2
  333. % where x1 y1 are the endpoint
  334.  
  335. /nodecurve {gsave
  336.             4 2 roll moveto
  337.             6 2 roll 4 2 roll curveto 
  338.             stroke
  339.             grestore} def
  340.  
  341. /arrownodecurve {gsave
  342.             4 2 roll moveto
  343.             6 2 roll 4 2 roll 4 copy 10 4 roll curveto 
  344.         gsave
  345.         newpath
  346.             arrowwidth arrowlength arrow
  347.             fill
  348.         grestore
  349.             stroke
  350.             grestore} def
  351.  
  352.  
  353. %% commands for drawing things around nodes
  354. % nodename nodebox -
  355. /nodebox {nodes exch get begin
  356.                    gsave
  357.                      newpath
  358.                      x y moveto
  359.              h w dobox
  360.             cleanup
  361.                  stroke
  362.                    grestore
  363.                   end} def
  364.  
  365.  
  366. % nodename nodecircle -
  367. % would be nice to have a parameter that adjusts diameter of circle
  368. % depth nodename nodecircle -
  369. /nodecircle {nodes exch get begin
  370.         gsave
  371.         newpath
  372.         w 2 div x add h 2 div y add 
  373.         w w mul h h mul add sqrt 2 div 4 -1 roll add
  374.         360 0 arcn 
  375.         cleanup
  376.         stroke
  377.         grestore
  378.         end} def
  379.  
  380. % no cleanup
  381. /nodecircletrans {nodes exch get begin
  382.         gsave
  383.         newpath
  384.         w 2 div x add h 2 div y add 
  385.         w w mul h h mul add sqrt 2 div 4 -1 roll add
  386.         360 0 arcn 
  387.         stroke
  388.         grestore
  389.         end} def
  390.  
  391. % nodename nodeoval -
  392. /nodeoval {nodes exch get begin
  393.     gsave
  394.     newpath
  395.     x 2 sub y 2 sub moveto 
  396.     h 4 add w 4 add dooval
  397.     cleanup
  398.     stroke
  399.     grestore
  400.     end } def
  401.  
  402. %  ratio depth nodename testnodeoval -
  403. %  ratio should be between 0 and 1
  404. /testnodeoval {nodes exch get begin
  405.     gsave
  406.     newpath
  407.         h h mul w w mul add sqrt div dup dup dup
  408.         w mul neg x add exch 
  409.         h mul neg y add 
  410.         moveto
  411.     dup
  412.         h mul 2 mul h add exch
  413.         w mul 2 mul w add  
  414.     testdooval
  415.     stroke
  416.     grestore
  417.     end } def
  418.  
  419.  
  420. /cleanup{gsave 
  421.     x y moveto
  422.     h nodemargin sub .5 add w nodemargin sub .5 add doccbox
  423.     1 setgray fill grestore
  424. }def
  425.  
  426. /boxdict 4 dict def
  427. boxdict /mtrx matrix put
  428.  
  429. /dobox{boxdict begin
  430.     /w exch def
  431.     /h exch def
  432.     /savematrix mtrx currentmatrix def
  433.     0 h rlineto
  434.         w 0 rlineto
  435.         0 h neg rlineto
  436.         closepath
  437.     savematrix setmatrix
  438.         end} def
  439.  
  440. % do box in counterclockwise fashion
  441. /doccbox{boxdict begin
  442.     /w exch def
  443.     /h exch def
  444.     /savematrix mtrx currentmatrix def
  445.     w 0 rlineto
  446.     0 h rlineto
  447.     w neg 0 rlineto
  448.     closepath
  449.     savematrix setmatrix
  450.     end} def
  451.  
  452.  
  453. /ovaldict 6 dict def
  454. ovaldict /mtrx matrix put
  455.  
  456. % h w ovaldict -
  457. /dooval{ovaldict begin
  458.     /w exch def
  459.     /h exch def
  460.     /savematrix mtrx currentmatrix def
  461.     0 h 2 div rmoveto
  462.     0 h 2 div nodemargin sub
  463.     nodemargin h 2 div
  464.     w 2 div h 2 div
  465.     rcurveto
  466.     w 2 div nodemargin sub 0
  467.         w 2 div nodemargin neg 
  468.     w 2 div h 2 div neg
  469.     rcurveto
  470.     0 h 2 div neg nodemargin add 
  471.     nodemargin neg h 2 div neg
  472.     w 2 div neg h 2 div neg
  473.     rcurveto
  474.     w 2 div neg nodemargin add 0
  475.     w 2 div neg nodemargin
  476.     w 2 div neg h 2 div 
  477.     rcurveto
  478.     savematrix setmatrix
  479.     end} def
  480.  
  481. % give height and width of a box and the lower left hand point
  482. % produce a curve that will enclose the box.
  483. % initial point already defined
  484. % ratio h w testdooval -
  485. /testdooval{ovaldict begin
  486.     /w exch def
  487.     /h exch def
  488.     /r exch def
  489.     h 2 div neg r mul dup 
  490.         h 2 div dup 
  491.         r mul 3 1 roll 
  492.         2 r sub mul
  493.     0 h
  494.     rcurveto
  495.     w 2 div dup
  496.         r mul exch
  497.         2 r sub mul 
  498.         w 2 div r mul dup 3 1 roll
  499.         w 0
  500.     rcurveto
  501.     h 2 div r mul dup
  502.         h 2 div neg dup
  503.         r mul 3 1 roll
  504.         2 r sub mul
  505.         0 h neg
  506.     rcurveto
  507.         w 2 div neg dup
  508.         r mul exch
  509.         2 r sub mul 
  510.         w 2 div neg r mul dup 3 1 roll
  511.         w neg 0
  512.     rcurveto
  513.     end} def
  514.  
  515. end % of treedict
  516.  
  517. end % of TeXdict
  518. % gsave
  519.